/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.actions;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import dbexel.model.dao.WorkSheetDao;
import dbexel.model.dao.WorkSheetDaoImpl;
import dbexel.model.dao.WorkSheetEntryDao;
import dbexel.model.dao.WorkSheetEntryDaoImpl;
import dbexel.model.mapping.AttributeValue;
import dbexel.model.mapping.WorkSheet;
import dbexel.model.mapping.WorkSheet_Attribute;
import dbexel.model.mapping.WorkSheet_Entry;

public class WorkSheetEntriesAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long workSheet_Id;

	private WorkSheet workSheet;

	private Map<Long, String> newAttributeValues = new HashMap<Long, String>();

	private Long selectedEntryId;
	
//	private Boolean deletionAcknowledged;//=Boolean.FALSE;

	private WorkSheetDao workSheetDao;
	private WorkSheetEntryDao workSheetEntryDao;


	/**
	 * @return the workSheet
	 */
	public WorkSheet getWorkSheet() {
		return workSheet;
	}

	/**
	 * @param in_workSheet
	 *            the workSheet to set
	 */
	public void setWorkSheet(WorkSheet in_workSheet) {
		workSheet = in_workSheet;
	}

	
	public String showWorkSheet() {
		setWorkSheet(getWorkSheetDao().fetchWorkSheet(getWorkSheet_Id()));
		
		return ActionSupport.SUCCESS;
		
	}

	public String create() {
		setWorkSheet(getWorkSheetDao().fetchWorkSheet(getWorkSheet_Id()));

		// First create a new entry
		WorkSheet_Entry l_newWSEntry = new WorkSheet_Entry();
		if (getNewAttributeValues().isEmpty()) {
			return ActionSupport.INPUT;
		}

		// Fetch the newly added entries
		// Parse the attributes of the worksheet
		for (WorkSheet_Attribute lt_wsAttribute : getWorkSheet()
				.getAttributes()) {
			if (getNewAttributeValues().containsKey(lt_wsAttribute.getAttrId())) {
				AttributeValue lt_attrValue = new AttributeValue(
						lt_wsAttribute, getNewAttributeValues().get(
								lt_wsAttribute.getAttrId()));
				l_newWSEntry.attachAttributeValue(lt_attrValue);
			}
		}
		getWorkSheet().addEntry(l_newWSEntry);
		
		getWorkSheetDao().updateWorkSheet(getWorkSheet());

		return ActionSupport.SUCCESS;
	}
	
	public String delete() {		
		
		WorkSheet_Entry l_wsEntry = getWorkSheetEntryDao().fetchWorkSheetEntry(getSelectedEntryId());
		setWorkSheet(l_wsEntry.getWorkSheet());
		
		getWorkSheetEntryDao().deleteWorkSheetEntry(l_wsEntry);		
		
		return ActionSupport.SUCCESS;
	}

	public String update() {
		setWorkSheet(getWorkSheetDao().fetchWorkSheet(getWorkSheet_Id()));

		if (getNewAttributeValues().isEmpty()) {
			
			return ActionSupport.INPUT;
		}

		WorkSheet_Entry l_wsEntry = null;
		for (WorkSheet_Entry lt_wsEntry : getWorkSheet().getEntries()) {
			if (lt_wsEntry.getEntryId().equals(getSelectedEntryId())) {
				l_wsEntry = lt_wsEntry;
			}
		}

		l_wsEntry.updateValues(getNewAttributeValues());
		getWorkSheetDao().updateWorkSheet(getWorkSheet());
		return ActionSupport.SUCCESS;
	}

	/**
	 * @param workSheet_Id
	 *            the workSheet_Id to set
	 */
	public void setWorkSheet_Id(Long workSheet_Id) {
		this.workSheet_Id = workSheet_Id;
	}

	/**
	 * @return the workSheet_Id
	 */
	public Long getWorkSheet_Id() {
		return workSheet_Id;
	}

	/**
	 * @param newAttributeValues
	 *            the newAttributeValues to set
	 */
	public void setNewAttributeValues(Map<Long, String> newAttributeValues) {
		this.newAttributeValues = newAttributeValues;
	}

	/**
	 * @return the newAttributeValues
	 */
	public Map<Long, String> getNewAttributeValues() {
		return newAttributeValues;
	}

	/**
	 * @param entryId
	 *            the entryId to set
	 */
	public void setSelectedEntryId(Long entryId) {
		this.selectedEntryId = entryId;
	}

	/**
	 * @return the entryId
	 */
	public Long getSelectedEntryId() {
		return selectedEntryId;
	}

	public void setWorkSheetDao(WorkSheetDao workSheetDao) {
		this.workSheetDao = workSheetDao;
	}

	public WorkSheetDao getWorkSheetDao() {
		if (workSheetDao==null)
			return  new WorkSheetDaoImpl();
		return workSheetDao;
	}

	/**
	 * @param workSheetEntryDao the workSheetEntryDao to set
	 */
	public void setWorkSheetEntryDao(WorkSheetEntryDao workSheetEntryDao) {
		this.workSheetEntryDao = workSheetEntryDao;
	}

	/**
	 * @return the workSheetEntryDao
	 */
	public WorkSheetEntryDao getWorkSheetEntryDao() {
		if (workSheetEntryDao==null)
			return new WorkSheetEntryDaoImpl();
		
		return workSheetEntryDao;
	}
}
